import org.junit.Test;

import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by leiylong on 2020/4/21
 */
public class dateTime {
    @Test
    public void testDateTime1() {
        Date date = new Date();
        System.out.println(date);
        System.out.println(date.toString());
    }

    @Test
    public void testDateTime2() {
        Date date = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println(ft.format(date));
    }

    @Test
    public void testDateTime3() {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d1 = df.parse("2020-04-22 12:04:57");
            Date d2 = df.parse("2020-04-21 12:04:57");
            long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
            // 1秒=1000微秒
            System.out.println(diff);
            long days = diff / (1000 * 60 * 60 * 24);
            System.out.println(days + "天");
            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            long seconds = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) /
                    (1000);
            System.out.println("" + days + "天" + hours + "小时" + minutes + "分" + seconds + "秒");
        } catch (Exception e) {
            System.out.println(e);

        }
    }

    @Test
    public void testDateTime4() throws Exception {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = ft.parse("2020-04-22 12:04:58");
        Date d2 = ft.parse("2020-04-22 12:04:57");
        long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
        System.out.println(diff);
        long seconds = diff / 1000;
        System.out.println(seconds + "秒");
        long minutes = diff / (1000 * 60);
        System.out.println(minutes + "分");
        long hours = diff / (1000 * 60 * 60);
        System.out.println(hours + "小时");
        long day = diff / (1000 * 60 * 60 * 24);
        System.out.println(day + "天");
    }

}
